package com.zjitc.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.net.httpserver.Authenticator.Success;
import com.zjitc.pojo.Car;
import com.zjitc.pojo.Cart;
import com.zjitc.pojo.User;
import com.zjitc.service.UserService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/19 15:09
 * @description:
 */
@Controller
public class UserController {
  private static final int countSize=4;//设置一页上有4条数据

  @Autowired
  private UserService userService;

  //注册
  @RequestMapping("/regist")
  public ModelAndView regist(@RequestParam("name")String name,
      @RequestParam("accountnumber")String accountnumber,
      @RequestParam("password")String password){
    ModelAndView modelAndView=new ModelAndView();
    //创建user对象

    User user = new User();
    user.setName(name);
    user.setAccountnumber(accountnumber);
    user.setPassword(password);
    userService.add(user);
    modelAndView.setViewName("redirect:/registsuccess.do");
    return modelAndView;
  }

  //登陆
  @RequestMapping("/login")
  public ModelAndView login(
      HttpSession session,
      HttpServletRequest request,
      @RequestParam("accountnumber") String accountnumber,
      @RequestParam("password") String password){
    ModelAndView modelAndView=new ModelAndView();
    System.out.println("name"+accountnumber);
    String inputCode=request.getParameter("code");
    String trueCode= (String) session.getAttribute("validateCode");
    if(!inputCode.equalsIgnoreCase(trueCode)){
      modelAndView.addObject("yzm","验证码错误");
      modelAndView.setViewName("/user/login.jsp");
      return modelAndView;
    }
    User user=userService.login(accountnumber,password);
    if(user==null){
      modelAndView.addObject("login","账号或密码错误");
      modelAndView.setViewName("/user/login.jsp");//转发
      return modelAndView;
    }
    session.setAttribute("user",user);
    modelAndView.setViewName("/showIndex.do");
    return modelAndView;
  }

  //退出
  @RequestMapping("/quit")
  public ModelAndView quit(HttpSession session){
    ModelAndView modelAndView=new ModelAndView();
    session.removeAttribute("user");
    modelAndView.setViewName("redirect:/showIndex.do");//重定向
    return modelAndView;
  }


  //车辆展示
  @RequestMapping("/showcar")
  public ModelAndView showcar(@RequestParam(value = "page",defaultValue = "1") Integer page){
    ModelAndView modelAndView = new ModelAndView();
    PageHelper.startPage(page,countSize);
    List<Car> carList=userService.findAll();
    PageInfo pageInfo=new PageInfo(carList);
    System.out.println(carList);
    modelAndView.addObject("pi",pageInfo);
    modelAndView.setViewName("/buycar.do");
    return modelAndView;
  }
  //gouwuche
  @RequestMapping("/add")
  public ModelAndView add(HttpServletRequest request){
    ModelAndView modelAndView = new ModelAndView();
    User user= (User) request.getSession().getAttribute("user");
    //如果未登录，则返回登录界面
    if (user==null){
      System.out.println(user);
      modelAndView.setViewName("/user/login.jsp");
      return modelAndView;
    }
    List<Cart> cartList=userService.findCart(user.getId());
    if(cartList.size()==0){
      modelAndView.setViewName("WEB-INF/homepage/cart.jsp");
       return modelAndView;
    }
    modelAndView.addObject("cartList",cartList);
    double total=0.00;
    for (Cart cart:cartList){
      total=total+cart.getNum()*cart.getCar().getCaroriginalprice();
    }
    modelAndView.addObject("total",total);
    modelAndView.setViewName("WEB-INF/homepage/cart.jsp");
    return modelAndView;
  }
  @RequestMapping("/shopcar")
  public ModelAndView shopcar(@RequestParam("carid") Integer carid){
    ModelAndView modelAndView = new ModelAndView();
    Car car=userService.findCarId(carid);
    modelAndView.addObject("c",car);
    modelAndView.setViewName("/productdetails.do");
    return modelAndView;
  }

  @RequestMapping("/insertdata")
  public ModelAndView insertdata(HttpSession session,Car car, Cart cart){
    ModelAndView modelAndView = new ModelAndView();
    User user= (User) session.getAttribute("user");
    //如果未登录，则返回登录界面
    if (user==null){
      System.out.println(user);
      modelAndView.setViewName("/user/login.jsp");
      return modelAndView;
    }
    Car car1=userService.findCarByCarId(car.getCarid());
    cart.setCar(car1);
    cart.setId(user.getId());
    userService.addtoCart(cart);
    System.out.println(cart);
    modelAndView.setViewName("redirect:/add.do");
    return modelAndView;
  }

  @RequestMapping("/deletecart")
  public ModelAndView deletecart(
      @RequestParam("cartid") String cartid
  ){
    ModelAndView modelAndView=new ModelAndView();
    System.out.println(cartid);
    userService.deletecart(cartid);
    modelAndView.setViewName("/add.do");
    return modelAndView;
  }

  @RequestMapping("/cleancart")
  public ModelAndView cleancart(HttpSession session){
    ModelAndView modelAndView=new ModelAndView();
    User user = (User) session.getAttribute("user");
    userService.clearCart(user.getId());
    modelAndView.setViewName("/add.do");
    return modelAndView;
  }



}
